﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VMC.Domain.Model.Service.Abstract;
using VMC.Domain.Model.Service;
using VMC.Domain.Model.DataStructure;
using System.IO;

namespace VMC.Web.Controllers.DataProcessor
{
    public class DPController : Controller
    {

        private IComplexDataTypeModelService iComplex;
        
        public DPController() : this(new ComplexDataTypeModelService()) { }
        public DPController(IComplexDataTypeModelService ComplexDataTypeService)
        {
            iComplex = ComplexDataTypeService;
        }
        //
        // GET: /DP/

        public ActionResult Index()
        {
            iComplex.UserName = User.Identity.Name;
            return View("Index", iComplex.GetList(User.Identity.Name));
        }

        public ActionResult Source(int ID)
        {
//            FileContentResult srcFile = new FileContentResult(null,
            UpdateSourceCode(ID);
            CreateSourceZipFile();
            ViewData["Source"] = "Source Code Updated";
            return File("J:\\source.zip","application/x-zip-compressed","Source.zip");
            //iComplex.UserName = User.Identity.Name;
            //return View("Index", iComplex.GetList(User.Identity.Name));
        }

        public ActionResult Binary(int ID)
        {
            GenerateBinary(ID);
            CreateBinaryZipFile();
            ViewData["Source"] = "New Binary generated";
            return File("J:\\dp.zip", "application/x-zip-compressed", "DataProcessor.zip");
            //iComplex.UserName = User.Identity.Name;
            //return View("Index", iComplex.GetList(User.Identity.Name));

        }

        private void CreateBinaryZipFile()
        {

            if (System.IO.Directory.Exists("J:\\dp"))
            {
                System.IO.Directory.Delete("J:\\dp", true);
            }

            System.IO.Directory.CreateDirectory("J:\\dp");


            string basepath = "J:\\jigar_work\\vmc mvc\\dataprocessor\\bin\\debug\\";
            string destpath = "J:\\dp\\";

            if (System.IO.File.Exists("j:\\dp.zip")) System.IO.File.Delete("j:\\dp.zip");


            System.IO.File.Copy(basepath + "dp.exe", destpath + "dp.exe");
            System.IO.File.Copy(basepath + "csvhelper.dll", destpath + "csvhelper.dll");

            Ionic.Zip.ZipFile srcFile = new Ionic.Zip.ZipFile();
            srcFile.AddDirectory(destpath);
            srcFile.Save("j:\\dp.zip");
        }
        private void CreateSourceZipFile()
        {
            if(System.IO.Directory.Exists("J:\\source"))
            {
                System.IO.Directory.Delete("J:\\source", true);
            }

            System.IO.Directory.CreateDirectory("J:\\source\\properties");
            //System.IO.Directory.CreateDirectory("J:\\source\\properties");
            System.IO.Directory.CreateDirectory("J:\\source\\bin\\debug");

            string basepath = "J:\\jigar_work\\vmc mvc\\dataprocessor\\";
            string destpath = "J:\\source\\";
            
            if (System.IO.File.Exists("j:\\source.zip")) System.IO.File.Delete("j:\\source.zip");

            
            System.IO.File.Copy(basepath + "program.cs", destpath + "program.cs");
            System.IO.File.Copy(basepath + "dataprocessor.csproj", destpath + "dataprocessor.csproj");
            System.IO.File.Copy(basepath + "datastructure.cs", destpath + "datastructure.cs");
            System.IO.File.Copy(basepath + "bin\\debug\\csvhelper.dll", destpath + "bin\\debug\\csvhelper.dll");
            System.IO.File.Copy(basepath + "Properties\\AssemblyInfo.cs", destpath + "Properties\\AssemblyInfo.cs");

            
            Ionic.Zip.ZipFile srcFile = new Ionic.Zip.ZipFile();
            srcFile.AddDirectory(destpath);
            srcFile.Save("j:\\source.zip");
        }
        private void UpdateSourceCode(int ID)
        {

            //Now Generate and replace source code for the class in data processor and then 
            IEnumerable<AddEditComplexDataTypeColumn> columns = iComplex.GetColumnsForEditing(ID, User.Identity.Name).ColumnManagement.Items.List;

            string classSource = "\n\t public class " + iComplex.GetUpdateModel(ID).AddEditModel.VariableName + "\n\t{\n";
            string enumSource = "\n \tpublic enum " + iComplex.GetUpdateModel(ID).AddEditModel.VariableName + "Order \n\t{\n";

            int i = 1;
            foreach (AddEditComplexDataTypeColumn column in columns)
            {
                classSource += "\t\tpublic " + column.BasicDataTypeSystemName + " " + column.VariableName + " {get;set;} \n";
                if (i == 1)
                {
                    enumSource += "\t\t" + column.VariableName + " = 1";
                }
                else
                {
                    enumSource += ",\n\t\t" + column.VariableName;
                }
                i = 0;
            }
            enumSource += "\n\t}\n";
            classSource += "\t}\n";


            StreamReader file = new StreamReader("J:\\JIGAR_WORK\\VMC MVC\\DataProcessor\\Program.template.txt");

            string newProgram = file.ReadToEnd().Replace("UserDataStructure", iComplex.GetUpdateModel(ID).AddEditModel.VariableName).Replace("<DataStructureEnumClass>", enumSource + classSource);
            //newProgram.Replace("<DataStructureEnumClass>", enumSource + classSource);
            file.Close();

            StreamWriter wFile = new StreamWriter("J:\\JIGAR_WORK\\VMC MVC\\DataProcessor\\Program.cs");
            wFile.Write(newProgram);
            wFile.Close();

            //wFile = new StreamWriter("J:\\JIGAR_WORK\\VMC MVC\\DataProcessor\\DataStructure.cs");
            //wFile.Write(enumSource + classSource);
            //wFile.Close();

        }


        private void GenerateBinary(int ID)
        {
            UpdateSourceCode(ID);
            //Now compile project and generate exe and send it to user;
            System.Diagnostics.ProcessStartInfo compiler = new System.Diagnostics.ProcessStartInfo();
            compiler.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe";
            compiler.Arguments = @"/reference:""j:\jigar_work\VMC MVC\dataprocessor\bin\debug\csvhelper.dll"" /out:""J:\jigar_work\VMC MVC\dataprocessor\bin\debug\dp.exe"" ""j:\JIGAR_WORK\VMC MVC\DataProcessor\Program.cs""";
            System.Diagnostics.Process.Start(compiler);
        }
    }
}


//namespace VMC.DataProcessor
//{
//    public enum UserDataStructureOrder
//    {
//        //This is auto generated
//        ///DataStructureEnum
//        Number = 1,
//        StationName,
//        Temp,
//        Wind
//    }

//    public class UserDataStructure
//    {
//        //This is auto generated
//        ///DataStructureClass
//        public int Number { get; set; }
//        public string StationName { get; set; }
//        public int Temp { get; set; }
//        public int Wind { get; set; }
//    }
//}
